package com.hmall.user.web;

import com.hmall.user.pojo.Address;
import com.hmall.user.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 根据地址id查询地址信息
     */
    @GetMapping("{addressId}")
    public Address findAddressById(@PathVariable("addressId") Long id) {
        return addressService.getById(id);
    }

    /**
     * 根据用户id查询地址列表
     */
    @GetMapping("/uid/{userId}")
    public List<Address> findAddressByUserId(@PathVariable("userId") Long userId) {
        return addressService.queryByUserId(userId);
    }

    /**
     * 添加地址信息
     *
     * @param address
     */
    @PostMapping("/add")
    public void addAddress(@RequestBody Address address) {
        addressService.addAddress(address);
    }

    /**
     * 删除地址信息
     *
     * @param id
     */
    @DeleteMapping("{id}")
    public void deleteAddress(@PathVariable Long id) {
        addressService.deleteAddress(id);
    }

    @PutMapping("/update")
    public void updateAddress(@RequestBody Address address) {
        addressService.updateAddress(address);
    }
}
